package com.ruoyi.web.controller.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.course.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailLoginController {

    @Autowired
    private RedisService redisService;

    /**
     * 邮箱验证码登录
     * @param email 用户邮箱
     * @param verificationCode 验证码
     * @return 登录结果
     */
    @PostMapping("/loginByEmail")
    public AjaxResult loginByEmail(@RequestParam String email, @RequestParam String verificationCode) {
        if (email == null || email.isEmpty() || verificationCode == null || verificationCode.isEmpty()) {
            return AjaxResult.error("邮箱和验证码不能为空");
        }

        // 校验验证码
        boolean result = redisService.verifyCodeAndLogin(email, verificationCode);
        if (result) {
            // 登录成功（可以在这里加入后续登录操作，比如创建 Session 或生成 JWT）
            return AjaxResult.success("登录成功");
        } else {
            return AjaxResult.error("验证码错误或已过期");
        }
    }
}
